package com.groupname.dangerousdelves;

import java.io.IOException;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer;
import android.content.Intent;
import android.os.IBinder;
import android.app.Service;

// -------------------------------------------------------------------------
/**
 *  Write a one-sentence summary of your class here.
 *  Follow it with additional details about its purpose, what abstraction
 *  it represents, and how to use it.
 *
 *  @author Christopher
 *  @version Nov 19, 2012
 */
public class GameMusic
    extends Service
{
    //~-------------------------------------------------------------------------
    // Member Variables
    private MediaPlayer mediaPlayer;
    public static boolean isRunning;

    //~-------------------------------------------------------------------------
    // Methods
    @Override
    public void onCreate()
    {
        super.onCreate();

        this.mediaPlayer = MediaPlayer.create(this,
            DungeonScreen.musicBuffer.next());

        this.mediaPlayer.setLooping(false);

        this.mediaPlayer.setVolume(100, 100);

        synchronized (DungeonScreen.screen)
        {
            this.mediaPlayer.setOnCompletionListener(DungeonScreen.screen);
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {
        try
        {
            this.mediaPlayer.start();
            isRunning = true;
        }
        catch(Exception e)
        {
            this.mediaPlayer.stop();
            isRunning = false;
        }

        return 1;
    }

    @Override
    public void onDestroy()
    {
        this.mediaPlayer.stop();
        this.mediaPlayer.release();
    }

    @Override
    public void onLowMemory()
    {
        this.mediaPlayer.stop();
    }

    @Override
    public IBinder onBind(Intent intent)
    {
        // TODO Auto-generated method stub
        return null;
    }
}
